package com.macro.mall.tiny.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.dto.RelationAttrInfoDTO;
import com.macro.mall.tiny.modules.pms.model.PmsProductAttribute;
import com.macro.mall.tiny.modules.pms.mapper.PmsProductAttributeMapper;
import com.macro.mall.tiny.modules.pms.model.PmsProductAttributeCategory;
import com.macro.mall.tiny.modules.pms.service.PmsProductAttributeCategoryService;
import com.macro.mall.tiny.modules.pms.service.PmsProductAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-08-15
 */
@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {

    @Autowired
    PmsProductAttributeMapper productAttributeMapper;
    @Autowired
    PmsProductAttributeCategoryService productAttributeCategoryService;

    @Override
    public List<RelationAttrInfoDTO> getRelationAttrInfoByPcId(Long pcId) {
        return productAttributeMapper.getRelationAttrInfoByCid(pcId);
    }

    @Override
    public Page<PmsProductAttribute> list(Long pacId, Integer pageSize, Integer pageNum, Integer type) {
        Page<PmsProductAttribute> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PmsProductAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProductAttribute::getProductAttributeCategoryId, pacId);
        queryWrapper.lambda().eq(PmsProductAttribute::getType, type);
        queryWrapper.lambda().orderByDesc(PmsProductAttribute::getSort);
        return page(page, queryWrapper);
    }

    @Override
    @Transactional
    public boolean delete(List<Long> ids) {
        PmsProductAttribute productAttribute = null;
        for (Long id : ids) {
            productAttribute = productAttributeMapper.selectById(id);
            if (productAttribute != null) {
                break;
            }
        }
        int deletedCnt = productAttributeMapper.deleteBatchIds(ids);
        if (deletedCnt > 0 && productAttribute != null) {
            PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttribute.getProductAttributeCategoryId());
            UpdateWrapper<PmsProductAttributeCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(PmsProductAttributeCategory::getId, productAttributeCategory.getId());
            if (productAttribute.getType() == 0) {
                updateWrapper.lambda().set(PmsProductAttributeCategory::getAttributeCount, productAttributeCategory.getAttributeCount()-deletedCnt);
            } else if (productAttribute.getType() == 1) {
                updateWrapper.lambda().set(PmsProductAttributeCategory::getParamCount, productAttributeCategory.getParamCount()-deletedCnt);
            }
            productAttributeCategoryService.update(updateWrapper);
        }

        return deletedCnt > 0;
    }

    @Override
    @Transactional
    public boolean create(PmsProductAttribute productAttribute) {
        boolean save = save(productAttribute);
        if (save) {
            PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttribute.getProductAttributeCategoryId());
            UpdateWrapper<PmsProductAttributeCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(PmsProductAttributeCategory::getId, productAttributeCategory.getId());
            if (productAttribute.getType() == 0) {
                updateWrapper.lambda().set(PmsProductAttributeCategory::getAttributeCount, productAttributeCategory.getAttributeCount()+1);
            } else if (productAttribute.getType() == 1) {
                updateWrapper.lambda().set(PmsProductAttributeCategory::getParamCount, productAttributeCategory.getParamCount()+1);
            }
            productAttributeCategoryService.update(updateWrapper);
        }
        return save;
    }
}
